package b2.scene.fight.skills
{
	import b2.config.*;
	import b2.scene.fight.PlayState;
	import b2.scene.fight.TankSprite;
	
	import com.greensock.*;
	
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class BirdsToPheonix extends BasicSkill
	{
		protected var areaArgs:Array;
		
		public function BirdsToPheonix(tankId:int, tankSide:int ,level:int, skillArgs:Object)
		{
			if(skillArgs["areas"] != null)
				areaArgs = skillArgs["areas"] as Array;
			super(tankId, tankSide ,level, skillArgs);
		}
		
		public override function discharge(x:int, y:int, direction:int):void
		{
			var skillArea:SkillArea = createMySkillArea(x, y, direction);
			
			playSkillMovie(x, y, direction);
			
			TweenLite.delayedCall(2, hitOtherTank, [x, y, tankId, skillArea]);
			
     		super.discharge(x, y, direction);
		}

		protected function createMySkillArea(x:int, y:int, direction:int):SkillArea
		{
			var skillArea:SkillArea = new SkillArea(x, y, direction);
			for (var i:int = 0; i < areaArgs.length; i++)
			{
				if (areaArgs[i]["areaIndex"] != null && areaArgs[i]["areaIndex"] == 1)
					skillArea.addCrossArea(areaArgs[i].front, areaArgs[i].back, areaArgs[i].left, areaArgs[i].right);
				else
					skillArea.addArea(areaArgs[i].front, areaArgs[i].back, areaArgs[i].left, areaArgs[i].right);
			}
			return skillArea;
		}
		
		protected function playSkillMovie(x:int, y:int, direction:int):void
		{
			var moviePosition:SkillPosition = new SkillPosition(x, y, direction);
			var tank:TankSprite = getPlayState().tanks[tankId];
			var point:Point = moviePosition.transPoint(0, 0);
			getPlayState().playFixMovie(12, int(point.x), int(point.y), direction, true);
		}
		
		protected function hitOtherTank(x:int, y:int, ownerId:int, skillArea:SkillArea):void
		{
			for (var tankIdStr:String in getPlayState().tanks)
			{
				var otherTank:TankSprite = getPlayState().tanks[tankIdStr];
				if (otherTank != null && this.side != otherTank.side && otherTank.side != TankSprite.SIDE_BASE)
				{
					if (skillArea.intersects(new Rectangle(otherTank.x + 2, otherTank.y + 2, PlayState.TILE_WIDTH - 4, PlayState.TILE_WIDTH - 4)))
					{
						onHit(x, y, otherTank);
					}
				}
			}
		}
		
		protected function onHit(x:int, y:int, tankOnHit:TankSprite):void
		{
			var areaDemage:int = level < skillArgs.areaDemage.length ? skillArgs.areaDemage[level] : skillArgs.areaDemage[0] * level;
			var effectDuration:int = level < skillArgs.effectDuration.length ? skillArgs.effectDuration[level] : skillArgs.effectDuration[0];
			var effectValue:int = level < skillArgs.effectValue.length ? skillArgs.effectValue[level] : skillArgs.effectValue[0];
			var effectValue2:int = level < skillArgs.effectValue2.length ? skillArgs.effectValue2[level] : skillArgs.effectValue2[0];
			getPlayState().updateTankStatus(tankOnHit.tankId, tankId, tankOnHit.currentHp, -areaDemage, 0, 0, true);
			getPlayState().addTankEffect(tankOnHit.tankId, tankId, skillArgs.effectIndex, effectDuration, effectValue, effectValue2);
			var targetX:Number = int((x - PlayState.OFFSET_X) / PlayState.TILE_WIDTH) * PlayState.TILE_WIDTH + PlayState.OFFSET_X;
			var targetY:Number = int((y - PlayState.OFFSET_Y) / PlayState.TILE_WIDTH) * PlayState.TILE_WIDTH + PlayState.OFFSET_Y;
			getPlayState().tankTweenLiteTo(tankOnHit.tankId, 500, tankOnHit.direction, tankOnHit.x, tankOnHit.y, targetX, targetY);
		}
	}
}